package com.woniu113.junk.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.order.client.dto.AdminInfoDTO;
import com.woniu113.junk.order.dto.AllOrderDTO;
import com.woniu113.junk.order.dto.AllOrderLogDTO;
import com.woniu113.junk.order.dto.FindDTO;
import com.woniu113.junk.order.dto.FindWaitingDTO;
import com.woniu113.junk.order.dto.findAdminIdDTO;
import com.woniu113.junk.order.exception.OrderInfoException;
import com.woniu113.junk.order.form.AllOrderLogForm;
import com.woniu113.junk.order.form.orderInfo.*;
import com.woniu113.junk.order.model.OrderInfo;
import com.woniu113.junk.order.model.OrderLog;
import com.woniu113.junk.order.param.AllOrderLogParam;
import com.woniu113.junk.order.param.FindAppealInfoParam;
import com.woniu113.junk.order.param.orderInfo.*;
import com.woniu113.junk.order.service.OrderInfoService;
import com.woniu113.junk.order.util.Data;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/orderInfo")
@Api(tags = "订单信息管理")
public class OrderInfoController {

    @Resource
    private OrderInfoService ois;

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 处理预约请求的接口
     *
     * @param form  包含预约信息的表单数据
     * @param token  用户身份验证的令牌
     * @return 返回一个包含预约结果的 Result 对象
     * @throws Exception 可能抛出的异常（如令牌解析失败等）
     * @author 小周
     * @date 2024/10/18
     */
    @PostMapping("/appointment")
    @ApiOperation(value = "处理预约请求", notes = "根据用户输入的预约信息进行预约")
    public Result<OrderInfo> appointment(
            @ApiParam(value = "预约信息表单", required = true) @RequestBody AddOrderInfoForm form,
            @ApiParam(value = "用户身份令牌", required = true) @RequestHeader String token) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        AddOrderInfoParam param = BeanUtil.toBean(form, AddOrderInfoParam.class);
        param.setUserId(userId);
        ois.appointment(param);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("预约成功");
    }

    /**
     * 处理接单请求的接口(废除)
     *
     * @param orderNum 订单编号
     * @param token    用户身份验证的令牌
     * @return 返回一个包含接单结果的 Result 对象
     * @throws Exception 可能抛出的异常（如令牌解析失败等）
     * @author 小周
     * @date 2024/10/18
     */
    @GetMapping("/acceptOrder/{orderNum}")
    @ApiOperation(value = "处理接单请求（回收小哥使用）", notes = "用户根据订单编号接单")
    public Result<OrderInfo> acceptOrder(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum,
            @ApiParam(value = "用户身份令牌", required = true) @RequestHeader String token) throws Exception {
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        ois.acceptOrder(orderNum, adminId);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("接单成功");
    }

    /**
     * 处理订单查询请求的接口
     *
     * @param form  包含查询骑手信息的表单数据
     * @return 返回一个包含能够接单的骑士信息的 Result 对象
     * @author 小周
     * @date 2024/10/24
     */
    @GetMapping("/findSuitableRider/{orderNum}")
    @ApiOperation(value = "查询能够接单的骑士信息", notes = "根据订单编号查找合适的骑士")
    public Result<Page<AdminInfoDTO>> findSuitableRider(@RequestBody FindSuitableRiderForm form) {
        Page<AdminInfoDTO> page = ois.findSuitableRider(BeanUtil.toBean(form, FindSuitableRiderParam.class));
        return Result.getPageResult(AdminInfoDTO.class).setCode(200).setMsg("查询能够接单的骑士信息成功").setData(page);
    }


    /**
     * 处理接单请求的接口（管理端使用）
     *
     * @param orderNum 订单编号
     * @param adminId 骑手ID
     * @return 返回一个包含接单结果的 Result 对象
     * @throws Exception 可能抛出的异常（如令牌解析失败等）
     * @author 小周
     * @date 2024/10/18
     */
    @GetMapping("/acceptOrderForAdmin")
    @ApiOperation(value = "处理接单请求（管理端使用）", notes = "管理员根据订单编号接单")
    public Result<OrderInfo> acceptOrderForAdmin(@RequestParam("orderNum") String orderNum,@RequestParam("adminId") Integer adminId) {
        System.out.println("订单编号"+orderNum);
        System.out.println("骑手"+adminId);
        ois.acceptOrderForAdmin(orderNum, adminId);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("骑手分配成功");
    }

    /**
     * 更新预约时间的接口
     *
     * @param form 包含更新信息的表单数据
     * @param token 用户身份令牌，用于验证用户身份
     * @return 返回一个包含更新结果的 Result 对象
     * @throws Exception 可能抛出的异常（如令牌解析失败等）
     * @author 小周
     * @date 2024/10/19
     */
    @PostMapping("/updateTime")
    @ApiOperation(value = "更新预约时间", notes = "根据用户输入的信息更新预约时间")
    public Result<OrderInfo> updateTime(
            @ApiParam(value = "更新预约时间表单", required = true) @RequestBody UpdateTimeForm form,
            @ApiParam(value = "用户身份令牌", required = true) @RequestHeader String token) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        UpdateTimeParam param = BeanUtil.toBean(form, UpdateTimeParam.class);
        param.setUserId(userId);
        ois.updateTime(param);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("修改预约时间成功");
    }

    /**
     * 取消预约的接口（用户取消）
     *
     * @param orderNum 订单编号
     * @param cancelReason 取消原因
     * @param token 用户身份令牌，用于验证用户身份
     * @return 返回一个包含取消结果的 Result 对象
     * @throws Exception 可能抛出的异常（如令牌解析失败等）
     * @author 小周
     * @date 2024/10/19
     */
    @GetMapping("/cancel/{orderNum}/{cancelReason}")
    @ApiOperation(value = "取消预约（用户取消）", notes = "用户根据订单编号和原因取消预约")
    public Result<OrderInfo> cancel(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum,
            @ApiParam(value = "取消原因", required = true) @PathVariable String cancelReason,
            @ApiParam(value = "用户身份令牌", required = true) @RequestHeader String token) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        ois.cancel(orderNum, userId, cancelReason);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("取消预约成功");
    }

    /**
     * 骑手开始上门取件的接口
     *
     * @param orderNum 订单编号
     * @return 返回一个包含上门取件结果的 Result 对象
     * @throws OrderInfoException 如果订单编号为空或订单不存在，抛出相应的异常
     * @author 小周
     * @date 2024/10/21
     */
    @GetMapping("/pickingUp")
    @ApiOperation(value = "骑手开始上门取件", notes = "骑手根据订单编号开始上门取件")
    public Result<OrderInfo> pickingUp(@RequestParam("orderNum") String orderNum) {
        System.out.println("订单编号"+orderNum);
        if (orderNum == null || orderNum.isEmpty()) throw new OrderInfoException("订单编号不能为空", 5302);
        OrderInfo orderInfo = ois.getById(orderNum);
        if (orderInfo == null || orderInfo.getOrderState() != Data.ORDER_STATE_RECEIVED) throw new OrderInfoException("订单不存在", 5303);
        orderInfo.setOrderState(Data.ORDER_STATE_PICKING_UP);
        ois.updateById(orderInfo);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("订单开启上门流程成功");
    }

    /**
     * 骑手到达目的地的接口
     *
     * @param orderNum 订单编号
     * @return 返回一个包含到达结果的 Result 对象
     * @throws OrderInfoException 如果订单编号为空或订单不存在，抛出相应的异常
     * @author 小周
     * @date 2024/10/21
     */
    @GetMapping("/reached")
    @ApiOperation(value = "骑手到达目的地", notes = "骑手根据订单编号确认到达目的地")
    public Result<OrderInfo> reached(@RequestParam("orderNum") String orderNum) {
        if (orderNum == null || orderNum.isEmpty()) throw new OrderInfoException("订单编号不能为空", 5302);
        OrderInfo orderInfo = ois.getById(orderNum);
        if (orderInfo == null || orderInfo.getOrderState() != Data.ORDER_STATE_PICKING_UP) throw new OrderInfoException("订单不存在", 5303);
        orderInfo.setOrderState(Data.ORDER_STATE_REACHED);
        ois.updateById(orderInfo);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("骑手小哥已到达成功");
    }

    /**
     * 取消预约的接口(骑手取消)
     *
     * @param orderNum 订单编号
     * @param cancelReason 取消原因
     * @param token 用户身份令牌，用于验证用户身份
     * @return 返回一个包含取消结果的 Result 对象
     * @throws Exception 可能抛出的异常（如令牌解析失败等）
     * @author 小周
     * @date 2024/10/21
     */
    @GetMapping("/cancelByRider/{orderNum}/{cancelReason}")
    @ApiOperation(value = "取消预约（骑手取消）", notes = "骑手根据订单编号和原因取消预约")
    public Result<OrderInfo> cancelByRider(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum,
            @ApiParam(value = "取消原因", required = true) @PathVariable String cancelReason,
            @ApiParam(value = "用户身份令牌", required = true) @RequestHeader String token) throws Exception {
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        ois.cancelByRider(orderNum, adminId, cancelReason);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("取消预约成功");
    }

    /**
     * 确认订单的接口(骑手确认)
     *
     * @param form 包含确认订单参数的 OkOderInfoForm 对象
     * @return 返回一个包含确认结果的 Result 对象
     * @throws Exception 可能抛出的异常（如参数转换失败等）
     * @author 小周
     * @date 2024/10/21
     */
    @PostMapping("/confirm")
    @ApiOperation(value = "确认订单", notes = "骑手确认订单")
    public Result<String> confirm(@RequestBody OkOderInfoForm form) throws IOException, AlipayApiException {
        System.out.println("订单编号"+form.getOrderNum());
        System.out.println(form.getMoney());
        System.out.println("废品长度"+form.getWasteList());
        String paymentUrl = ois.confirm(BeanUtil.toBean(form, OkOderInfoParam.class));
        // 返回支付链接给前端
        System.out.println(paymentUrl);
        return Result.getResult(String.class).setCode(200).setMsg("确认订单成功").setData(paymentUrl);
    }

    /**
     * 正常处理订单废品入库的接口
     *
     * @param orderNum  订单编号
     * @return 返回一个包含订单信息的 Result 对象
     * @throws OrderInfoException 当订单不存在、已删除或状态不符合入库条件时抛出该异常
     * @author 小周
     * @date 2024/10/22
     */
    @PostMapping("/stock/{orderNum}")
    @ApiOperation(value = "处理订单废品入库", notes = "正常处理订单废品入库")
    public Result<OrderInfo> stock(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum) {
        OrderInfo orderInfo = ois.getById(orderNum);
        if (orderInfo == null || orderInfo.getState() == Data.ORDER_INFO_STATE_DELETE || orderInfo.getOrderState() != Data.STOCK_STATE_NOT_IN_STOCK)
            throw new OrderInfoException("订单不存在", 5301);
        orderInfo.setStockState(Data.STOCK_STATE_IN_STOCK);
        ois.updateById(orderInfo);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("订单废品入库成功");
    }

    /**
     * 处理订单的异常入库情况的接口
     *
     * @param orderNum       订单编号
     * @param abnormalReason 异常原因
     * @return 返回一个包含订单信息的 Result 对象
     * @throws OrderInfoException 当订单不存在、已删除或状态不符合入库条件时抛出该异常
     * @author 小周
     * @date 2024/10/22
     */
    @PostMapping("/abnormalStock/{orderNum}/{abnormalReason}")
    @ApiOperation(value = "处理订单异常入库情况", notes = "异常处理订单入库情况")
    public Result<OrderInfo> abnormalStock(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum,
            @ApiParam(value = "异常原因", required = true) @PathVariable String abnormalReason) {
        OrderInfo orderInfo = ois.getById(orderNum);
        if (orderInfo == null || orderInfo.getState() == Data.ORDER_INFO_STATE_DELETE || orderInfo.getOrderState() != Data.STOCK_STATE_NOT_IN_STOCK)
            throw new OrderInfoException("订单不存在", 5301);
        orderInfo.setStockState(Data.STOCK_STATE_VERIFICATION_FAILED);
        orderInfo.setAbnormalReason(abnormalReason);
        ois.updateById(orderInfo);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("订单废品入库成功");
    }

    /**
     * 处理订单评价的接口
     *
     * @param orderNum  订单编号
     * @param score     评价分数
     * @return 返回一个包含订单信息的 Result 对象
     * @throws OrderInfoException 当订单不存在或已被删除时抛出该异常
     * @author 小周
     * @date 2024/10/18
     */
    @GetMapping("/evaluate/{orderNum}/{score}")
    @ApiOperation(value = "处理订单评价", notes = "用户对订单进行评价")
    public Result<OrderInfo> evaluate(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum,
            @ApiParam(value = "评价分数", required = true) @PathVariable Integer score) {
        OrderInfo orderInfo = ois.getById(orderNum);
        if (orderInfo == null || orderInfo.getState() == Data.ORDER_INFO_STATE_DELETE)
            throw new OrderInfoException("订单不存在", 5301);
        orderInfo.setScore(score);
        orderInfo.setOrderState(Data.ORDER_STATE_IN_EVALUATED);
        ois.updateById(orderInfo);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("订单信息状态改为已评价成功");
    }

    /**
     * 处理订单删除的接口
     *
     * @param orderNum  订单编号
     * @return 返回一个包含订单信息的 Result 对象
     * @throws OrderInfoException 当订单不存在或已被删除时抛出该异常
     * @author 小周
     * @date 2024/10/18
     */
    @GetMapping("/delete/{orderNum}")
    @ApiOperation(value = "处理订单删除", notes = "用户删除指定订单")
    public Result<OrderInfo> delete(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum) {
        OrderInfo orderInfo = ois.getById(orderNum);
        if (orderInfo == null || orderInfo.getState() == Data.ORDER_INFO_STATE_DELETE)
            throw new OrderInfoException("订单不存在", 5301);
        orderInfo.setState(Data.ORDER_INFO_STATE_DELETE);
        ois.updateById(orderInfo);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("订单信息状态改为删除成功");
    }

    /**
     * 查询符合条件的预约订单的接口
     *
     * @param form  查询条件的表单对象，包含过滤参数
     * @return 返回一个包含预约订单信息的分页 Result 对象
     * @throws Exception 可能抛出异常，具体取决于实现细节
     * @author 小周
     * @date 2024/10/22
     */
    @PostMapping("/findAppointment")
    @ApiOperation(value = "查询符合条件的预约订单", notes = "根据查询条件获取预约订单")
    public Result<Page<OrderInfo>> findAppointment(
            @ApiParam(value = "查询条件表单", required = true) @RequestBody FindAppointmentForm form) {
        FindAppointmentParam param = BeanUtil.toBean(form, FindAppointmentParam.class);
        Page<OrderInfo> page = ois.findAppointment(param);
        return Result.getPageResult(OrderInfo.class).setCode(200).setMsg("查询符合条件预约订单成功").setData(page);
    }

    /**
     * 根据订单编号查询订单信息的接口
     *
     * @param orderNum  订单编号
     * @return 返回一个包含订单信息的 Result 对象
     * @throws OrderInfoException 当订单不存在、已被删除或状态不符合条件时抛出该异常
     * @author 小周
     * @date 2024/10/22
     */
    @GetMapping("/findById/{orderNum}")
    @ApiOperation(value = "根据订单编号查询订单信息", notes = "根据订单编号获取订单详情")
    public Result<OrderInfo> findById(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum) {
        OrderInfo orderInfo = ois.getById(orderNum);
        if (orderInfo == null || orderInfo.getState() == Data.ORDER_INFO_STATE_DELETE || orderInfo.getStockState() != Data.STOCK_STATE_NOT_IN_STOCK)
            throw new OrderInfoException("订单不存在", 5301);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("查询符合条件预约订单成功").setData(orderInfo);
    }

    /**
     * 查询符合条件的订单特定废品类型的重量.
     *
     * @param orderNum 订单编号
     * @param typeId 类型 ID
     * @return Result<Double> 返回查询结果，包括预约订单的重量
     * @author 小周
     * @date 2024/10/22
     */
    @GetMapping("/queryWeight/{orderNum}/{typeId}")
    @ApiOperation(value = "查询订单特定废品类型的重量", notes = "根据订单编号和类型ID查询重量")
    public Result<Double> queryWeight(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum,
            @ApiParam(value = "类型 ID", required = true) @PathVariable Integer typeId) {
        Double weight = ois.queryWeight(orderNum, typeId);
        return Result.getResult(Double.class).setCode(200).setMsg("查询符合条件订单特定废品类型的重量成功").setData(weight);
    }

    /**
     * 查询符合条件的骑手
     * */
    @PostMapping("/findAdmin")
    @ApiOperation(value = "查询符合条件的骑手", notes = "查询符合条件的骑手")
    public Result<List<AdminInfoDTO>> findAdminId(@RequestBody FindByStationForm form){
        List<AdminInfoDTO> adminInfoDTOS = ois.findAdmin(BeanUtil.toBean(form,FindByStationParam.class));
        return Result.getListResult(AdminInfoDTO.class).setData(adminInfoDTOS).setMsg("查询符合条件的骑手成功").setCode(200);
    }

    /**
     * 查询符合条件的预约单
     * */
    @PostMapping("/findWaiting")
    @ApiOperation(value = "查询所有预约订单", notes = "查询所有预约订单")
    public Result<List<OrderInfo>> findWaiting(@RequestBody FindWaitingDTO dto) {
        Integer stationId = dto.getStationId();
        Integer pageSize = dto.getPageSize();
        Integer pageNum = dto.getPageNum();
        String redisKeyPrefix = "waste:orderInfo:";
        Set<String> keys = stringRedisTemplate.keys(redisKeyPrefix + "*");
        List<OrderInfo> orderInfos = new ArrayList<>();
        // 从 Redis 中获取所有预约订单信息
        for (String key : keys) {
            Map<Object, Object> orderData = stringRedisTemplate.opsForHash().entries(key);
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setOrderNum((String) orderData.get("order_num"));
            orderInfo.setUserId(Integer.valueOf((String) orderData.get("user_id")));
            orderInfo.setUserPhone((String) orderData.get("user_phone"));
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            orderInfo.setStartTime(LocalDateTime.parse((String) orderData.get("start_time"), formatter));
            orderInfo.setEndTime(LocalDateTime.parse((String) orderData.get("end_time"), formatter));
            orderInfo.setCreateTime(LocalDateTime.parse((String) orderData.get("create_time"), formatter));
            orderInfo.setOrderInfo((String) orderData.get("order_info"));
            orderInfo.setStationId(Integer.valueOf((String) orderData.get("station_id")));
            orderInfo.setStationName((String) orderData.get("station_name"));
            orderInfo.setStationPhone((String) orderData.get("station_phone"));
            orderInfo.setState(Integer.valueOf((String) orderData.get("state")));
            // 条件过滤
            if (stationId == null || orderInfo.getStationId().equals(stationId)) {
                orderInfos.add(orderInfo);
            }
        }
        // 进行分页
        int totalRecords = orderInfos.size();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, totalRecords);
        // 检查索引范围
        if (fromIndex > totalRecords) {
            fromIndex = totalRecords;
            toIndex = totalRecords;
        }
        List<OrderInfo> pagedOrderInfos = orderInfos.subList(fromIndex, toIndex);
        // 创建结果对象
        Result<List<OrderInfo>> result = Result.getListResult(OrderInfo.class);
        result.setData(pagedOrderInfos);
        result.setTotal(totalRecords);
        return result;
    }

    /**
     * 接单
     * */
    @GetMapping("/orderTaking")
    @ApiOperation(value = "接单", notes = "接单")
    public Result<OrderInfo> orderTaking(@RequestParam("orderNum") String orderNum,@RequestHeader String token) throws Exception {
        int admin = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        ois.orderTaking(orderNum,admin);
        return Result.getResult(OrderInfo.class).setCode(200).setMsg("接单成功");
    }


    @PostMapping("/findAll")
    @ApiOperation(value = "查询所有订单")
    private Result<List<OrderInfo>> findAll(@RequestBody AllOrderForm form){
        System.out.println("分页数据1"+form.getPageNum());
        System.out.println("分页数据2"+form.getPageSize());
        System.out.println("状态"+form.getOrderState());
        System.out.println("入库状态"+form.getOrderState());
        System.out.println("订单编号"+form.getOrderNum());
        // 计算查询的起始索引
        int offset = (form.getPageNum() - 1) * form.getPageSize();
        AllOrderDTO dto = ois.findAll(BeanUtil.toBean(form, AllOrderParam.class),offset);
        Integer total = dto.getTotal();
        return Result.getListResult(OrderInfo.class).setData(dto.getList()).setMsg("查询所有订单成功").setCode(200).setTotal(dto.getTotal());
    }

    @PostMapping("/findAllByRiderId")
    @ApiOperation(value = "查询当前登录骑手的所有订单")
    private Result<List<OrderInfo>> findAllByRiderId(@RequestBody AllOrderForm form,@RequestHeader String token) throws Exception {
        System.out.println("分页数据1"+form.getPageNum());
        System.out.println("分页数据2"+form.getPageSize());
        System.out.println("状态"+form.getOrderState());
        System.out.println("入库状态"+form.getOrderState());
        System.out.println("订单编号"+form.getOrderNum());
        int admin = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        // 计算查询的起始索引
        int offset = (form.getPageNum() - 1) * form.getPageSize();
        AllOrderDTO dto = ois.findAllByRiderId(BeanUtil.toBean(form, AllOrderParam.class),offset,admin);
        Integer total = dto.getTotal();
        return Result.getListResult(OrderInfo.class).setData(dto.getList()).setMsg("查询当前登录骑手的所有订单成功").setCode(200).setTotal(dto.getTotal());
    }

    /**
     * 查询当前登录用户回收订单
     * */
    @PostMapping("/find")
    @ApiOperation(value = "查询当前登录用户回收订单", notes = "查询当前登录用户回收订单")
    public Result<List<OrderInfo>> find(@RequestBody FindForm form,@RequestHeader String token) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        FindParam param = BeanUtil.toBean(form, FindParam.class);
        param.setUserId(userId);
        FindDTO dto = ois.find(param);
        return Result.getListResult(OrderInfo.class).setCode(200).setMsg("查询当前登录用户回收订单").setData(dto.getList()).setTotal(dto.getTotal());
    }

    /**
     * 查询当前站长站点的订单
     */
    @PostMapping("/findOrderByStationMasterId")
    @ApiOperation(value = "查询当前站长站点的订单", notes = "查询当前站长站点的订单")
    public Result<List<OrderInfo>> findOrderByStationMasterId(@RequestBody AllOrderForm form,@RequestHeader String token) throws Exception {
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        AllOrderParam param = BeanUtil.toBean(form, AllOrderParam.class);
        int offset = (form.getPageNum() - 1) * form.getPageSize();
        AllOrderDTO dto = ois.findOrderByStationMasterId(param, offset, adminId);
        return Result.getListResult(OrderInfo.class).setCode(200).setMsg("查询当前站长站点的订单成功").setData(dto.getList()).setTotal(dto.getTotal());
    }
}

